package job3;

//线程的一种实现方式，继承Thread
public class SaleThread extends Thread {
    // 使用复合复用原则，共享 火车票类对象 资源
    private TrainTickets trainTickets;
    // 用于控制卖票的速度
    private int delay;
    // 卖票员姓名
    private String name;
    public SaleThread(TrainTickets trainTickets, String name, int delay) {
        this.trainTickets = trainTickets;
        this.name = name;
        this.delay = delay;
    }
    //    覆盖run方法，卖票
    @Override
    public void run() {
//        如果成功卖票，则可持续卖下去
        while (trainTickets.sellTickets(name)) {
            try {
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
